/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fabdialogmorph.helper;

import io.fabdialogmorph.utils.ColorUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * AnimationHelper
 *
 * @since 2021-05-14
 */
public class AnimationHelper {
    private static final float FLOAT_005 = 0.1f;
    private static final int NUM_2 = 2;
    private static final int NUM_3 = 3;
    int endColor;
    private int cornerRadius;
    private final int duration = 1000;
    private AnimatorProperty dialogAnimatorValue;
    private AnimatorProperty allAnimatorValue;
    private AnimatorProperty srcollAnimatorValue;
    private AnimatorProperty endAnimatorValue;
    private AnimatorProperty endDialogAnimatorValue;
    private boolean isShowDialog = false;
    private Context context;
    private float alpha;
    private boolean startflag;

    /**
     * AnimationHelper
     *
     * @param context context
     */
    public AnimationHelper(Context context) {
        this.context = context;
    }

    /**
     * revealMenu
     *
     * @param animatorGroup animatorGroup
     * @param viewBean      viewBean
     * @param dialogView    dialogView
     * @param component     component
     * @param end           end
     */
    public void revealMenu(AnimatorGroup animatorGroup, ViewBean viewBean, ComponentContainer dialogView,
                           Component component, AnimationGroupEnd end) {
        dialogAnimatorValue = dialogView.createAnimatorProperty();
        allAnimatorValue = viewBean.getDlMain().createAnimatorProperty();
        srcollAnimatorValue = dialogView.createAnimatorProperty();
        endAnimatorValue = dialogView.createAnimatorProperty();
        endDialogAnimatorValue = dialogView.createAnimatorProperty();
        dialogAnimatorValue.setDuration(duration);
        allAnimatorValue.setDuration(duration);
        srcollAnimatorValue.setDuration(duration);
        endAnimatorValue.setDuration(duration);
        endDialogAnimatorValue.setDuration(duration);
        checkIf(viewBean, dialogView, component);
        dialogAnimatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
                if (!isShowDialog) {
                    component.setVisibility(Component.VISIBLE);
                }
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                end.animationGroupEnd();

            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        exeListener(animatorGroup, end);
        animatorGroup.runParallel(dialogAnimatorValue, allAnimatorValue, srcollAnimatorValue);
        animatorGroup.start();
    }


    private void exeListener(AnimatorGroup animatorGroup, AnimationGroupEnd end) {
        animatorGroup.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                end.animationGroup();
                end.animationGroupEnd();

            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
    }

    private void checkIf(ViewBean viewBean, ComponentContainer dialogView, Component component) {
        if (isShowDialog) {
            dialogView.setContentPositionX(viewBean.getLocationX());
            dialogView.setContentPositionY(viewBean.getLocationY());
            dialogView.setVisibility(Component.VISIBLE);
            dialogView.setScale(0, 0);
            dialogView.setPivotX(viewBean.getLocationX());
            dialogView.setPivotY(viewBean.getLocationY());
            dialogAnimatorValue.moveToX(viewBean.getMoveX()).moveToY(viewBean.getMoveY());
            dialogAnimatorValue.scaleX(1).scaleY(1).alpha(1);
            allAnimatorValue.alpha(1);
        } else {
            dialogView.setScale(1, 1);
            dialogView.setPivotX(viewBean.getMoveX());
            dialogView.setPivotY(viewBean.getMoveY());

            dialogAnimatorValue.scaleX(0).scaleY(0).alpha(1);
            dialogAnimatorValue.moveFromX(dialogView.getContentPositionX())
                    .moveToX(component.getContentPositionX() - component.getWidth() / NUM_2)
                    .moveFromY(dialogView.getContentPositionY()).moveToY(component.getContentPositionY()
                    - (viewBean.getWindowFloat().getHeight() / NUM_3 * NUM_2 + viewBean
                    .getWindowFloat().getMarginBottom()));
            allAnimatorValue.alpha(1);
        }
    }

    /**
     * createAnimator
     *
     * @param dialogView dialogView
     * @param allview    allview
     */
    public void createAnimator(ComponentContainer dialogView, ComponentContainer allview) {
        dialogAnimatorValue = dialogView.createAnimatorProperty();
        allAnimatorValue = allview.createAnimatorProperty();
        srcollAnimatorValue = dialogView.createAnimatorProperty();
        endAnimatorValue = dialogView.createAnimatorProperty();
        endDialogAnimatorValue = dialogView.createAnimatorProperty();
        dialogAnimatorValue.setDuration(duration);
        allAnimatorValue.setDuration(duration);
        srcollAnimatorValue.setDuration(duration);
        endAnimatorValue.setDuration(duration);
        endDialogAnimatorValue.setDuration(duration);
    }

    /**
     * setViewLocation
     *
     * @param dialogView dialogView
     * @param slFab      slFab
     */
    public void setViewLocation(ComponentContainer dialogView, ComponentContainer slFab) {
        float sfFabWidth = slFab.getContentPositionX() + slFab.getWidth() / NUM_2;
        float sfFabHeight = slFab.getContentPositionY() + slFab.getHeight() / NUM_2;
    }

    /**
     * showOverlay
     *
     * @param animatorValue animatorValue
     * @param end           end
     * @param overlayLayout overlayLayout
     * @return boolean showOverlay
     */
    public boolean showOverlay(AnimatorValue animatorValue, Component overlayLayout, AnimationGroupEnd end) {
        animatorValue.setDuration(duration);
        animatorValue.setValueUpdateListener((animatorValue1, v) -> {
            overlayLayout.setTag(animatorValue);
            overlayLayout.setAlpha(v);
            if (v >= FLOAT_005) {
                overlayLayout.setVisibility(Component.VISIBLE);
            }
        });
        animatorValue.start();
        animatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                end.animationGroup();
                end.animationGroupEnd();
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        return true;
    }

    /**
     * closeView
     *
     * @param closeLayout closeLayout
     */
    public void closeView(Component closeLayout) {
        AnimatorValue animatorValue = (AnimatorValue) closeLayout.getTag();
        if (animatorValue != null) {
            animatorValue.cancel();
        }
        AnimatorProperty animatorProperty = closeLayout.createAnimatorProperty();
        animatorProperty.alpha(0).setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                closeLayout.setAlpha(0);
                closeLayout.setVisibility(Component.INVISIBLE);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorProperty.start();
    }

    /**
     * hideOverlay
     *
     * @param overlayLayout overlayLayout
     */
    public void hideOverlay(Component overlayLayout) {
        AnimatorValue animatorValue = (AnimatorValue) overlayLayout.getTag();
        if (animatorValue != null) {
            animatorValue.cancel();
        }
        AnimatorProperty animatorProperty = overlayLayout.createAnimatorProperty();
        animatorProperty.alpha(0).setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                overlayLayout.setAlpha(0);
                overlayLayout.setVisibility(Component.INVISIBLE);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        animatorProperty.start();
    }

    /**
     * isShowView
     *
     * @param isShowDia isShowDia
     * @return boolean
     */
    public boolean isShowView(boolean isShowDia) {
        this.isShowDialog = isShowDia;
        return isShowDia;
    }

    /**
     * getCornerRadius
     *
     * @return getCornerRadius
     */
    public float getCornerRadius() {
        return cornerRadius;
    }

    /**
     * getDuration
     *
     * @return duration 返回动画持续时间
     */
    public int getDuration() {
        return duration;
    }

    /**
     * getAlpha
     *
     * @return float
     */
    public float getOpacity() {
        return alpha;
    }

    public void setEndColor(int endColor) {
        this.endColor = endColor;
    }

    /**
     * setAlpha
     *
     * @param alpha alpha
     */
    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    /**
     * getRgbColor
     *
     * @param color color
     * @return RgbColor
     */
    public RgbColor getRgbColor(int color) {
        return new RgbColor(ColorUtils.redInt(color),
                ColorUtils.greenInt(color), ColorUtils.blueInt(color), Color.alpha(color));
    }

    /**
     * setColor
     *
     * @param color color
     */
    public void setColor(int color) {
        RgbColor rgbColor = new RgbColor(ColorUtils.redInt(color),
                ColorUtils.greenInt(color), ColorUtils.blueInt(color), Color.alpha(color));
    }
}
